import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/config/lf_type_config.dart';
import 'package:largefriends/entity/entities/type_config_list_entity.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_fonts.dart';
import 'package:largefriends/res/lf_text_style.dart';
import 'package:largefriends/res/lf_widget_ext.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/utils/lf_dialog_utils.dart';
import 'package:largefriends/utils/lf_scroll_behavior.dart';

showLfSinglePicker<T>({
  required LfConfigTypeEnum configTypeEnum,
  required int value,
  String? title,
  required Function(int value) saveCallback,
  Function()? cancelCallback,
  bool isDismissible = true,
}) {
  RxInt selectedValue = value.obs;
  var configs = TypeConfig().getTypeConfigListEntity(configTypeEnum);
  List<TypeConfigEntity> temp = [];
  temp.addAll(configs.data ?? []);

  Get.bottomSheet(
    WillPopScope(
      child: Container(
        height: ScreenUtil().screenHeight / 2,
        padding: EdgeInsets.symmetric(horizontal: 16.w),
        decoration: BoxDecoration(
          color: color000000,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(14.r),
            topRight: Radius.circular(14.r),
          ),
        ),
        child: Column(
          children: [
            Row(
              children: [
                InkWell(
                  onTap: () {
                    hideSheet();
                    cancelCallback?.call();
                  },
                  child: Padding(
                    padding:
                        EdgeInsets.only(top: 10.h, bottom: 10.h, right: 10.w),
                    child: Text(
                      strCancel.tr,
                      style: lfTextStyle(sp: 18.sp, fontFamily: fontMedium,color: color514F62),
                    ),
                  ),
                ),
                10.w.sbw,
                Expanded(
                  child: Text(
                    title ?? "",
                    textAlign: TextAlign.center,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: lfTextStyle(sp: 18.sp, fontFamily: fontBold),
                  ).shadeMask,
                ),
                10.w.sbw,
                Obx(() {
                  bool enable = selectedValue.value > 0;
                  return InkWell(
                      onTap: () {
                        if (enable) {
                          hideSheet();
                          saveCallback(selectedValue.value);
                        }
                      },
                      child: Padding(
                        padding: EdgeInsets.only(
                            top: 10.h, bottom: 10.h, left: 10.w),
                        child: Text(
                          strDone.tr,
                          style: lfTextStyle(
                              sp: 18.sp,
                              fontFamily: fontMedium,
                              color: enable
                                  ? colorFF007F
                                  : Colors.transparent),
                        ),
                      ));
                })
              ],
            ),
            Container(
              height: 2.h,
              width: ScreenUtil().screenWidth,
              color: color8F8F8F,
            ),
            10.h.sbh,
            Expanded(
                child: ScrollConfiguration(
              behavior: LfScrollBehavior(),
              child: SingleChildScrollView(
                child: Column(
                    children: temp
                        .map((e) => Obx(() {
                              bool isSelected = e.key == selectedValue.value;
                              return InkWell(
                                onTap: () {
                                  selectedValue.value = e.key;
                                },
                                child: Padding(
                                  padding: EdgeInsets.symmetric(vertical: 8.h),
                                  child: Row(
                                    children: [
                                      Expanded(
                                          child: Text(
                                        e.value,
                                        style: lfTextStyle(
                                            sp: 16.sp,
                                            fontFamily: fontMedium,
                                            color: isSelected
                                                ? colorFF007F
                                                : colorFEFEFE),
                                      )),
                                      10.w.sbw,
                                      Opacity(
                                        opacity: isSelected == true ? 1 : 0,
                                        child: Icon(
                                          Icons.check_circle,
                                          color: colorFF007F,
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              );
                            }))
                        .toList()),
              ),
            ))
          ],
        ),
      ),
      onWillPop: () async {
        Get.back();
        return isDismissible;
      },
    ),
    enableDrag: isDismissible,
    isDismissible: isDismissible,
  );
}
